package switch表达式;

/**
 * 全新的switch表达式，原先是语句
 * 主要的区别是表达式有返回值，而语句则没有。
 */
public class SwitchDemo {
    /**
     * JDK8中获取switch返回值方式
     */
    public static int getByJDK8(Week week){
        int i=0;
        switch (week){
            case MONDAY:
                i=1;
                break;
            case TUESDAY:
                i=2;
                break;
            case WEDNESDAY:
                i=3;
                break;
            case THURSDAY:
                i=4;
                break;
            case FRIDAY:
                i=5;
                break;
            case SATURDAY:
                i=6;
                break;
            default:
                i=7;
                break;
        }
        return i;
    }
    public static int getByJDK17(Week week){
         int day= switch (week){
            case null->-1;
            case MONDAY -> 1;
            case TUESDAY -> 2;
            case WEDNESDAY -> 3;
            case THURSDAY -> 4;
            case FRIDAY -> 5;
            case SATURDAY -> 6;
            default -> 7;
         };
         return day;
    }

    /**
     * switch yield的用法可以理解为把switch()当作一个方法去使用，将方法的返回语句return改为yield返回，
     * 此时必须有一个变量在外边接收返回的这个值
     * 此时switch作为一条语句。所以末尾要加分号
     *
     * @param week
     * @return
     */
    public static int getByJDK17Demo(Week week){
        int day=switch (week){
            case null:
                yield -1;
            case MONDAY:
                yield 1;
            case TUESDAY:
                yield 2;
            case WEDNESDAY:
                yield 3;
            case THURSDAY:
                yield 4;
            case FRIDAY:
                yield 5;
            case SATURDAY:
                yield 6;
            default:
                yield 7;
        };
        return day;
    }

    public static void main(String[] args) {
        System.out.println(getByJDK17(Week.MONDAY));
        System.out.println(getByJDK17Demo(Week.MONDAY));
    }
}
